***********************************************************************************
*** REPLICATION CODE FOR THE ANALYSES IN "RETHINKING OCCUPATIONAL TASK CONTENT" ***
******** BY HASLBERGER (2021) IN THE ECONOMIC AND LABOUR RELATIONS REVIEW *********
***********************************************************************************

***************
*** FIGURES ***
***************

use "task_content_elrr.dta", clear //adjust file path

/*FIGURE 1*/
preserve
label drop _all
graph dot rscore2d_EU27 cscore2d_EU27, over(isco_88_2, label(labsize(medsmall) alt)) ///
 note(See appendix for list of ISCO-88 codes at the 2-digit level) ysize(8.5) xsize(12) ///
 yscale(range(-1 1)) graphregion(color(white)) ///
 title("Routine intensity and task complexity at ISCO 2-digit level", color(black)) ///
 subtitle("in the EU-27 countries") l2title("2-digit ISCO-88 occupations") ///
 legend(label(1 "Routine-intensity score") label(2 "Task-complexity score"))
restore

/*FIGURE 2*/
preserve
duplicates drop isco_88_2, force
pwcorr rpercentile_w3_EU15 adpercentile_w3_EU15, sig
regress rpercentile_w3_EU15 adpercentile_w3_EU15 [aw=mean_wempshare_w3_EU15]
local r2_a: display %5.3f e(r2_a)

label drop _all
twoway (scatter rpercentile_w3_EU15 adpercentile_w3_EU15 [aw=mean_wempshare_w3_EU15], msymbol(circle_hollow) mc(dknavy) mlw(thin)) ///
	(scatter rpercentile_w3_EU15 adpercentile_w3_EU15, msymbol(i) mlabel(isco_88_2) mlabsize(vsmall) mlabc(dknavy) mlabpos(0)) ///
	(lfit rpercentile_w3_EU15 adpercentile_w3_EU15 [aw=mean_wempshare_w3_EU15], lc(red)), xsize(12) ysize(15) aspect(1) /// 
	note("adjusted R-squared = `r2_a'" "Sample: EU15, occupations weighted by employment in wave 3") ///
	title("Comparing my RTI index with AD", color(black) size(medlarge)) graphregion(color(white)) ///
	legend(label(1 "ISCO-88 2-digit occupations") label(2 "") label(3 "Fitted values") size(small)) ///
	xtitle("Percentile on AD RTI index", size(small)) ytitle("Percentile on MH RTI index", size(small)) 
restore

/*FIGURE 3*/
preserve
duplicates drop isco_88_2, force
pwcorr rpercentile_w3_EU15 rpercentile_fmh, sig
regress rpercentile_w3_EU15 rpercentile_fmh [aw=mean_wempshare_w3_EU15]
local r2_a: display %5.3f e(r2_a)

label drop _all
twoway (scatter rpercentile_w3_EU15 rpercentile_fmh [aw=mean_wempshare_w3_EU15], msymbol(circle_hollow) mc(dknavy) mlw(thin)) ///
	(scatter rpercentile_w3_EU15 rpercentile_fmh, msymbol(i) mlabel(isco_88_2) mlabsize(vsmall) mlabc(dknavy) mlabpos(0)) ///
	(lfit rpercentile_w3_EU15 rpercentile_fmh [aw=mean_wempshare_w3_EU15], lc(red)), xsize(12) ysize(15) aspect(1) /// 
	note("adjusted R-squared = `r2_a'" "Sample: EU15, occupations weighted by employment in wave 3") ///
	title("Comparing my RTI index with FMH", color(black) size(medlarge)) graphregion(color(white)) ///
	legend(label(1 "ISCO-88 2-digit occupations") label(2 "") label(3 "Fitted values") size(small)) ///
	xtitle("Percentile on FMH RTI index", size(small)) ytitle("Percentile on MH RTI index", size(small)) 
restore

/*FIGURE 4*/
preserve
duplicates drop isco_88_2, force
pwcorr cpercentile_w3_EU15 cpercentile_fmh, sig
regress cpercentile_w3_EU15 cpercentile_fmh [aw=mean_wempshare_w3_EU15]
local r2_a: display %5.3f e(r2_a)

label drop _all
twoway (scatter cpercentile_w3_EU15 cpercentile_fmh [aw=mean_wempshare_w3_EU15], msymbol(circle_hollow) mc(dknavy) mlw(thin)) ///
	(scatter cpercentile_w3_EU15 cpercentile_fmh, msymbol(i) mlabel(isco_88_2) mlabsize(vsmall) mlabc(dknavy) mlabpos(0)) ///
	(lfit cpercentile_w3_EU15 cpercentile_fmh [aw=mean_wempshare_w3_EU15], lc(red)), xsize(12) ysize(15) aspect(1) /// 
	note("adjusted R-squared = `r2_a'" "Sample: EU15, occupations weighted by employment in wave 3") ///
	title("Comparing my complexity index" "with the FMH cognitive index", color(black) size(medlarge)) graphregion(color(white)) ///
	legend(label(1 "ISCO-88 2-digit occupations") label(2 "") label(3 "Fitted values") size(small)) ///
	xtitle("Percentile on FMH cognitive index", size(small)) ytitle("Percentile on MH RTI index", size(small)) 
restore

/*FIGURE 5*/
preserve
tostring isco_88_2, gen(isco_88_s)
egen isco_n = group(isco_88_s), label

twoway (scatter isco_n arscore2d_w_EU15 if wave==3, m(t) /*msa(180)*/ mc(sand)) ///
	(rcap arscore2d_w_EU15_lci arscore2d_w_EU15_hci isco_n if wave==3, hor lc(sand) lw(medthin)) ///
	(scatter isco_n arscore2d_w_EU15 if wave==6, m(t) /*msa(180)*/ mc(sienna)) ///
	(rcap arscore2d_w_EU15_lci arscore2d_w_EU15_hci isco_n if wave==6, hor lc(sienna) lw(medthin)), ///
	graphregion(color(white)) xsize(12) ysize(12) title("Routine task intensity", color(black) size(medlarge)) ///
	ytitle("2-digit ISCO-88 occupations") xsc(r(-1(.5)1)) xlab(#5, gstyle(minor)) ysc(rev) ylab(#27, val labs(small) angle(0)) ///
	legend(label(1 "2000 point estimate") label(2 "95% confidence interval") label(3 "2015 point estimate") label(4 "95% confidence interval") size(small)) saving(routine, replace)

twoway (scatter isco_n acscore2d_w_EU15 if wave==3, m(t) /*msa(180)*/ mc(sand)) ///
(rcap acscore2d_w_EU15_lci acscore2d_w_EU15_hci isco_n if wave==3, hor lc(sand) lw(medthin)) ///
(scatter isco_n acscore2d_w_EU15 if wave==6, m(t) /*msa(180)*/ mc(sienna)) ///
(rcap acscore2d_w_EU15_lci acscore2d_w_EU15_hci isco_n if wave==6, hor lc(sienna) lw(medthin)), ///
	graphregion(color(white)) xsize(10) ysize(12) title("Task complexity", color(black) size(medlarge)) ///
	ytitle("") xsc(r(-1(.5)1)) xlab(-1[.5]1, gstyle(minor)) ysc(rev alt) ylab(#27, val labs(small) angle(0)) saving(complexity, replace)

grc1leg2 routine.gph complexity.gph, leg(routine.gph) graphregion(color(white)) ///
title("Changes in task intensity, 2000 - 2015", color(black) size(medlarge)) ///
note("Sample: EU-15 countries, scores for waves 3 and 6 based on the pooled dataset", margin(medium))
restore

/*FIGURE 6*/
preserve
keep if EU15==1
by isco_88_2 wave, sort: egen mean_wempshare = mean(wempshare)
duplicates drop isco_88_2 wave, force
sort isco_88_2 wave
gen ardelta = .
replace ardelta = (arscore2d_w_EU15 - arscore2d_w_EU15[_n-1]) if wave[_n-1]!=6
gen acdelta = .
replace acdelta = (acscore2d_w_EU15 - acscore2d_w_EU15[_n-1]) if wave[_n-1]!=6
pwcorr ardelta acdelta [w=mean_wempshare]

regress ardelta acdelta [aw=mean_wempshare]
local r2_a: display %5.3f e(r2_a)

label drop _all
twoway (scatter ardelta acdelta [aw=mean_wempshare], msymbol(circle_hollow) mc(dknavy) mlw(thin)) ///
	(scatter ardelta acdelta, msymbol(i) mlabel(isco_88_2) mlabsize(vsmall) mlabc(dknavy) mlabpos(0)) /// 
	(lfit ardelta acdelta [aw=mean_wempshare], lc(red)), xsize(12) ysize(10) ///
	note("adjusted R-squared = `r2_a'" "Sample: EU-15, occupations weighted by average employment in each wave") ///
	title("Changes in RTI and task complexity", color(black) size(large)) graphregion(color(white)) ///
	legend(label(1 "ISCO-88 2-digit occupations") label(2 "") label(3 "Fitted values") size(medsmall)) ///
	xtitle("Change in task complexity", size(medsmall)) ytitle("Change in RTI", size(medsmall)) 
restore

**************
*** TABLES ***
**************

use "task_content_elrr.dta", clear //adjust file path

/*TABLE 3*/
spearman rpercentile_w3_EU15 cpercentile_w3_EU15 adpercentile_w3_EU15 rpercentile_fmh cpercentile_fmh

/*TABLE 4*/
//Country averages and rank order correlations
preserve
keep if EU15==1 & isco_88_2!=.
by country, sort: sum rscore2d_cs [w=wempshare]
by country, sort: sum cscore2d_cs [w=wempshare]
//create variables with the rank of each occupation in cs dataset
duplicates drop country isco_88_2, force
by country, sort: egen rscore_rank = rank(rscore2d_cs), unique
by country, sort: egen cscore_rank = rank(cscore2d_cs), unique
levelsof country, local(countries)
foreach c in `countries' {
gen rrank_`c' = .
replace rrank_`c' = rscore_rank if country=="`c'"
gen crank_`c' = .
replace crank_`c' = cscore_rank if country=="`c'"
}
//collapse and produce correlation matrices
collapse (mean) rrank* crank* rscore2d_EU15 cscore2d_EU15, by(isco_88_2)
egen rrank = rank(rscore2d_EU15), unique
egen crank = rank(cscore2d_EU15), unique
estpost correlate rrank*, matrix
est store c1
esttab * using rrank.rtf, replace b(a2) unstack not noobs compress nostar
estpost correlate crank*, matrix nohalf
est store c1
esttab * using crank.rtf, replace b(a2) unstack not noobs compress nostar
restore

/*TABLE 2*/
/*Reproducing this table requires the original EWCS individual data 
which is available on UK Data Servce.*/
use "ewcs_1991-2015_ukda_5mar2018.dta"

/*Create individual task intensity scores for each worker*/
//First, create dummy for observations with information on all index variables and occupation
gen complete = 0
replace complete = 1 if (y15_Q30e==7|y15_Q30e==6|y15_Q30e==5|y15_Q30e==4|y15_Q30e==3|y15_Q30e==2|y15_Q30e==1) & ///
(y15_Q48a==1|y15_Q48a==2) & (y15_Q48b==1|y15_Q48b==2) & (y15_Q53a==1|y15_Q53a==2) & (y15_Q53d==1|y15_Q53d==2) & ///
(y15_Q30i==7|y15_Q30i==6|y15_Q30i==5|y15_Q30i==4|y15_Q30i==3|y15_Q30i==2|y15_Q30i==1) & (y15_Q53c==1|y15_Q53c==2) & ///
(y15_Q53e==1|y15_Q53e==2) & (y15_Q53f==1|y15_Q53f==2) & y15_ISCO_88_2!=.
*keep if complete==1

//RTI index

local varlist EU27 EU15 
foreach var of local varlist {
*Code question 30e: "Does your main job involve repetitive hand or arm movements?" 
*From 1: all of the time to 7: never
gen routine_`var'_1 =.
replace routine_`var'_1 = 1 if y15_Q30e==7 & `var'==1
replace routine_`var'_1 = 2 if y15_Q30e==6 & `var'==1
replace routine_`var'_1 = 3 if y15_Q30e==5 & `var'==1
replace routine_`var'_1 = 4 if y15_Q30e==4 & `var'==1
replace routine_`var'_1 = 5 if y15_Q30e==3 & `var'==1
replace routine_`var'_1 = 6 if y15_Q30e==2 & `var'==1
replace routine_`var'_1 = 7 if y15_Q30e==1 & `var'==1

*Reverse code question 48: "Does your job involve short repetitive tasks of less than 1 minute/10 minutes?"
*1: yes; 2: no
gen routine_`var'_2a =.
replace routine_`var'_2a = 1 if y15_Q48a==1 & `var'==1
replace routine_`var'_2a = 0 if y15_Q48a==2 & `var'==1

gen routine_`var'_2b =.
replace routine_`var'_2b = 1 if y15_Q48b==1 & `var'==1
replace routine_`var'_2b = 0 if y15_Q48b==2 & `var'==1

*Reverse code question 53a: "Generally, does your main paid job involve meeting precise quality standards?"
*1: yes; 2: no
gen routine_`var'_3 =.
replace routine_`var'_3 = 1 if y15_Q53a==1 & `var'==1
replace routine_`var'_3 = 0 if y15_Q53a==2 & `var'==1

*Reverse code question 53d: "Generally, does your main paid job involve monotonous tasks?"
*1: yes; 2: no
gen routine_`var'_4 =.
replace routine_`var'_4 = 1 if y15_Q53d==1 & `var'==1
replace routine_`var'_4 = 0 if y15_Q53d==2 & `var'==1

//Complexity index

*Code question 30i: "Does your main paid job involve working with computers, tablets, smartphones, etc.?"
*From 1: all of the time to 7: never
gen complex_`var'_1 =.
replace complex_`var'_1 = 1 if y15_Q30i==7 & `var'==1
replace complex_`var'_1 = 2 if y15_Q30i==6 & `var'==1
replace complex_`var'_1 = 3 if y15_Q30i==5 & `var'==1
replace complex_`var'_1 = 4 if y15_Q30i==4 & `var'==1
replace complex_`var'_1 = 5 if y15_Q30i==3 & `var'==1
replace complex_`var'_1 = 6 if y15_Q30i==2 & `var'==1
replace complex_`var'_1 = 7 if y15_Q30i==1 & `var'==1

*Code question 53c: "Generally, does your main paid job involve solving unforeseen problems on your own?"
*1: yes; 2: no
gen complex_`var'_2 =.
replace complex_`var'_2 = 1 if y15_Q53c==1 & `var'==1
replace complex_`var'_2 = 0 if y15_Q53c==2 & `var'==1

*Code question 53e: "Generally, does your main paid job involve complex tasks?"
*1: yes; 2: no
gen complex_`var'_3 =.
replace complex_`var'_3 = 1 if y15_Q53e==1 & `var'==1
replace complex_`var'_3 = 0 if y15_Q53e==2 & `var'==1

*Code question 53f: "Generally, does your main paid job involve learning new things?"
*1: yes; 2: no
gen complex_`var'_4 =.
replace complex_`var'_4 = 1 if y15_Q53f==1 & `var'==1
replace complex_`var'_4 = 0 if y15_Q53f==2 & `var'==1

//Standardize variables
egen zroutine_`var'_1 = std(routine_`var'_1) if complete==1
egen zroutine_`var'_2a = std(routine_`var'_2a) if complete==1
egen zroutine_`var'_2b = std(routine_`var'_2b) if complete==1
egen zroutine_`var'_3 = std(routine_`var'_3) if complete==1
egen zroutine_`var'_4 = std(routine_`var'_4) if complete==1
egen zcomplex_`var'_1 = std(complex_`var'_1) if complete==1
egen zcomplex_`var'_2 = std(complex_`var'_2) if complete==1  
egen zcomplex_`var'_3 = std(complex_`var'_3) if complete==1
egen zcomplex_`var'_4 = std(complex_`var'_4) if complete==1

*weigh scores for each worker
egen rscore_`var' = rowmean(zroutine_`var'_*) 
egen cscore_`var' = rowmean(zcomplex_`var'_*) 

drop routine* zroutine* complex* zcomplex*
}

//ANOVA to check within-/between-variation
loneway rscore_EU27 y15_ISCO_88_2 [w=w4]
loneway cscore_EU27 y15_ISCO_88_2 [w=w4]
